import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbfdusergroup } from '../../../../atoms/db';

const logger = anylogger('pages/api/admin/groups/addug.api');

export type Result = {
	ok: true;
} | {
	ok: false;
	message: string;
};

export type Message = {
	groupid: string;
	userid: string[];
}

/**
 * 新增用户组别关联服务-ITbfdusergroup('fdusergroup')
 */
const handler = an48<Result>();

handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const { groupid, userid } = req.body as Message;
		//存在选中数据
		if (userid.length > 0
		) {
			await tbfdusergroup().insert(userid.map((it) => {
				return {
					userid: it,
					groupid: groupid
				};
			}));
		}
		res.status(200).json({ ok: true });
	} catch (error) {
		logger.error(error);
		logger.trace(error);
		res.status(200).json({ ok: false, message: (error as Error).message });
	}
});

export const config = {} as PageConfig;

export default handler;
